import requests
import os
from bs4 import BeautifulSoup

# 要爬取的网页url
url = 'https://www.zhihu.com/question/430097652/answer/1655676155'

# 设置请求头部信息，避免被反爬虫机制识别
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

# 获取网页内容
response = requests.get(url, headers=headers)
content = response.content.decode('utf-8')

# 解析网页，提取图片url
img_urls = []
soup = BeautifulSoup(content, 'html.parser')
for img in soup.find_all('img'):
    try:
        img_url = img['data-actualsrc']
        img_urls.append(img_url)
    except:
        pass

# 创建目录用于存储图片
if not os.path.exists('images'):
    os.mkdir('images')

# 下载图片
for i, img_url in enumerate(img_urls):
    response = requests.get(img_url)
    with open('images/img{}.jpg'.format(i), 'wb') as f:
        f.write(response.content)
